#include "my_notepad.h"

my_notepad::my_notepad(QWidget *parent) : QMainWindow(parent)
{
    init_notepad();
}

void my_notepad::init_notepad()
{
    my_Bar = menuBar();
    file_menu = my_Bar->addMenu("文件");
    edit_menu = my_Bar->addMenu("编辑");
    look_menu = my_Bar->addMenu("查看");

    // 1. 新建
    QAction *newAct = file_menu->addAction(QIcon(":/1.jpg"), "新建");
    newAct->setShortcut(QKeySequence("Ctrl+N")); // 设置快捷键 Ctrl+N
    file_menu->addSeparator();
    // 2. 打开
    QAction *openAct = file_menu->addAction(QIcon(":/1.jpg"), "打开");
    openAct->setShortcut(QKeySequence("Ctrl+O")); // 设置快捷键 Ctrl+O
    file_menu->addSeparator();
    // 3. 保存
    QAction *saveAct = file_menu->addAction(QIcon(":/1.jpg"),"保存");
    saveAct->setShortcut(QKeySequence("Ctrl+S")); // 设置快捷键 Ctrl+S
    file_menu->addSeparator();
    // 4. 另存为
    QAction *saveAsAct = file_menu->addAction(QIcon(":/1.jpg"),"另存为");
    saveAsAct->setShortcut(QKeySequence("Ctrl+Shift+S")); // 设置快捷键 Ctrl+Shift+S
    file_menu->addSeparator();
    // 5. 退出
    QAction *exitAct = file_menu->addAction(QIcon(":/1.jpg"),"退出");
    exitAct->setShortcut(QKeySequence("Ctrl+Q")); // 设置快捷键 Ctrl+Q

    // 编辑菜单
    QAction *copyAct = edit_menu->addAction("复制");
    copyAct->setShortcut(QKeySequence("Ctrl+c"));
    edit_menu->addSeparator();

    QAction *pasteAct = edit_menu->addAction("粘贴");
    pasteAct->setShortcut(QKeySequence("Ctrl+V"));
    edit_menu->addSeparator();
    QAction *insertTimestampAct = edit_menu->addAction("插入时间戳");
    insertTimestampAct->setShortcut(QKeySequence("Ctrl+T"));

    //查看菜单
    QMenu *zoom = look_menu->addMenu("缩放");
    look_menu->addSeparator();
    QAction *status_barAct = look_menu->addAction("状态栏");
    QAction *largenAct = zoom->addAction("变大");
    largenAct->setShortcut(QKeySequence("Ctrl+k"));
    zoom->addSeparator();
    QAction *lessenAct = zoom->addAction("变小");
    lessenAct->setShortcut(QKeySequence("Ctrl+l"));
    look_menu->addSeparator();
    QAction *dtime = look_menu->addAction("LCD显示时间");

    //设置状态栏
    s_Bar = statusBar();
    //默认左侧显示，但是会覆盖
    //    s_Bar->showMessage("这是一个状态栏");
    //    s_Bar->showMessage("left");
    //可见不可见
    //s_Bar->setVisible(false);
#if 0
    //在左侧显示组件,不会覆盖
    s_Bar->addWidget(new QLabel("left"));
    s_Bar->addWidget(new QLabel("left1"));

    //在右侧显示组件,不会覆盖
    s_Bar->addPermanentWidget(new QLabel("right"));
    s_Bar->addPermanentWidget(new QLabel("right2"));
#endif
#if 0
    //1-使用直接显示时间在状态栏
    s_Bar->showMessage(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    //QTimer定时器
    timer = new QTimer(this);
    //使用lambda
    connect(timer,&QTimer::timeout,[=](){
         s_Bar->showMessage(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    });
    timer->start(100);
#endif
    //2-使用文本框lable在状态栏显示时间
    timeLabel = new QLabel;
    timeLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    s_Bar->addWidget(timeLabel);
    //QTimer定时器 QTime
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        timeLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    });
    timer->start(100);
    //3-使用LCD显示时间
    connect(dtime,&QAction::triggered,[=](){
        lcd = new QLCDNumber;
        lcd->setDigitCount(8);//设置界面显示多个字符
        connect(timer,&QTimer::timeout,[=](){
            lcd->display(QDateTime::currentDateTime().toString("hh:mm:ss"));
        });
        lcd->show();
    });
    timer->start(100);








    //设置中心---多行文本框
    edit = new QTextEdit;
    setCentralWidget(edit);

    // 连接槽函数
    //     connect(newAct, &QAction::triggered, this, &my_notepad::newFile);
    //     connect(openAct, &QAction::triggered, this, &my_notepad::openFile);
    connect(saveAct, &QAction::triggered, this, &my_notepad::saveFile);
    //     connect(saveAsAct, &QAction::triggered, this, &my_notepad::saveFileAs);
    connect(exitAct, &QAction::triggered, this, &my_notepad::exitApp);

    connect(copyAct, &QAction::triggered, this, &my_notepad::copyText);
    connect(pasteAct, &QAction::triggered, this, &my_notepad::pasteText);
    connect(insertTimestampAct, &QAction::triggered, this, &my_notepad::insertTimestamp);

    connect(status_barAct, &QAction::triggered, this, &my_notepad::toggleStatusBar);
    connect(largenAct, &QAction::triggered, this, &my_notepad::zoomIn);
    connect(lessenAct, &QAction::triggered, this, &my_notepad::zoomOut);

}

void my_notepad::saveFile()
{
    saveFile();
}

void my_notepad::exitApp()
{
    close();
}

void my_notepad::copyText()
{
    edit->copy();
}

void my_notepad::pasteText()
{
    edit->paste();
}

void my_notepad::insertTimestamp()
{

    // 获取时间 QTime
    QString tm = (QTime::currentTime ().toString ("hh:mm:ss"));
    edit->append (tm);
    // 获取日期 QDate
    edit->append (QDate::currentDate ().toString ("yyyy-MM-dd"));
    // 获取时间日期 QDateTime
    edit->append (QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

    //edit->insertPlainText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}

void my_notepad::toggleStatusBar()
{
    QStatusBar *statusBar = findChild<QStatusBar *>();
    if (statusBar) {
        statusBar->setVisible(!statusBar->isVisible()); // 切换状态栏显示状态
    }
}

void my_notepad::zoomIn()
{
    int currentSize = edit->fontPointSize();
    edit->setFontPointSize(currentSize + 1);
}

void my_notepad::zoomOut()
{
    int currentSize = edit->fontPointSize();
    edit->setFontPointSize(currentSize - 1);
}
























